package main

import (
	"fmt"
	"reflect"
	"time"
)

type DateTime struct {
	year   int
	month  int
	mday   int
	hour   int
	minute int
	second int
}

func GetCurrentDateTime() DateTime {
	var cur time.Time = time.Now()
	var s DateTime
	s.year = cur.Year()
	s.month = int(cur.Month())
	s.mday = cur.Day()
	s.hour = cur.Hour()
	s.minute = cur.Minute()
	s.second = cur.Second()
	return s
}

func newDateTime(year int, month int, mday int, hour int, minute int, second int) DateTime {
	var dt DateTime
	dt.year = year
	dt.month = month
	dt.mday = mday
	dt.hour = hour
	dt.minute = minute
	dt.second = second
	return dt
}

func newDateTimeFromString(s string) DateTime {
	var dt DateTime
	fmt.Sscanf(s, "%d-%d-%d %d:%d:%d", &dt.year, &dt.month, &dt.mday, &dt.hour, &dt.minute, &dt.second)
	return dt
}

func (s DateTime) String() string {
	return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", s.year, s.month, s.mday, s.hour, s.minute, s.second)
}

func (s DateTime) time_t() int64 {
	var a time.Time
	var err error
	a, err = time.Parse("2006-01-02 15:04:05", s.String())
	if err != nil {
		return 0
	} else {
		return int64(a.Unix())
	}
}

func main() {
	tcur := time.Now()
	to := reflect.TypeOf(tcur)
	fmt.Println(to, to.Kind(), tcur)

	var dt DateTime = GetCurrentDateTime()
	fmt.Println("now:", dt)

	dt = newDateTime(2020, 1, 1, 0, 0, 0)
	fmt.Println("2020年1月1日零点零分零秒:", dt)

	dt2 := newDateTimeFromString("2020-01-01 00:01:02")
	fmt.Println(dt2, ",对应时间戳:", dt2.time_t())

	a, erra := time.Parse("2006-01-02 15:04:05", "2020-01-01 00:01:02")
	b, errb := time.Parse("2006-01-02 15:04:05", "2021-02-03 04:06:06")
	if erra != nil || errb != nil {
		fmt.Println("转化字符串到time.Time失败！")
	} else {
		fmt.Println("b=", b, ",a = ", a)
		var c time.Duration = b.Sub(a)
		fmt.Println("c = (b-a) =", c)
		fmt.Println("c: h m s = ", c.Hours(), c.Minutes(), c.Seconds())

		fmt.Println("a + c = ", a.Add(c))
	}
}
